/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 *
 * Copyright (C) 2005 Robert KLEIN. robeklein@gmail.com * 
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.process;

import java.sql.Timestamp;

import org.compiere.apps.ProcessCtl;
import org.compiere.model.MPInstance;
import org.compiere.model.MProcess;
import org.compiere.model.X_A_Asset;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Trx;

/**
 *	Asset Internal Movement
 *	
 *  @author Duman Zhunissov
 *  @version $Id: A_InternalMovement.java,v 1.0 $
 */
public class A_InternalMovement extends SvrProcess
{
	/** Asset ID				*/
	private int p_A_Asset_ID = 0;
	private Timestamp p_DateMoved;
	private int p_C_BPartnerSR_ID=0;
	private String p_Description="";
	private String p_DocNo="";
	private int p_A_Location_ID = 0;
	public String repName = "A_Asset_Form_OS2";
	
	/**
	 *  Prepare - e.g., get Parameters.
	 */
	protected void prepare()
	{
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("A_Asset_ID"))
				p_A_Asset_ID = para[i].getParameterAsInt();
			else if (name.equals("DateMoved")) {
				p_DateMoved = (Timestamp)para[i].getParameter();
			}	
			else if (name.equals("C_BPartnerSR_ID"))
				p_C_BPartnerSR_ID = para[i].getParameterAsInt();
			else if (name.equals("Description"))
				p_Description = para[i].getParameter().toString();
			else if (name.equals("DocumentNo"))
				p_DocNo = para[i].getParameter().toString();
			else if (name.equals("A_Location_ID"))
				p_A_Location_ID = para[i].getParameterAsInt();
			else
				log.info("prepare - Unknown Parameter: " + name);
		}
		if(p_DocNo==null)
			p_DocNo="";
	}	//	prepare

	
	/**
	 *	@return info
	 *	@throws Exception
	 */
	protected String doIt() throws java.lang.Exception
	{
		log.info("doIt - Asset_ID=" + p_A_Asset_ID);
		if (p_A_Asset_ID == 0)
			throw new IllegalArgumentException("No Record");
		
		//
		int no = 0;
		X_A_Asset asset = new X_A_Asset (getCtx(), p_A_Asset_ID, get_TrxName());
		
		no = asset.TransferTo(p_DocNo, p_DateMoved, p_C_BPartnerSR_ID, p_Description,p_A_Location_ID);
		if (no>0) {
			Timestamp currdate = new Timestamp(System.currentTimeMillis());
			int bpsr = asset.getC_BPartnerSR_ID(currdate);
			asset.setC_BPartnerSR_ID(bpsr);
			asset.save();
			//	Prepare Process
			int AD_Process_ID = MProcess.getProcess_ID(repName, Trx.createTrxName());
			MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0);
			if (!instance.save())
			{
				log.severe(Msg.getMsg(Env.getCtx(), "ProcessNoInstance"));
			}
			ProcessInfo pi = new ProcessInfo ("", AD_Process_ID);
			pi.setAD_PInstance_ID (instance.getAD_PInstance_ID());
			// Create instance parameters. I e the parameters you want to send to the process.
			ProcessInfoParameter pi1 = new ProcessInfoParameter("AD_Org_ID", asset.getAD_Org_ID(), "","","");
			ProcessInfoParameter pi2 = new ProcessInfoParameter("A_Asset_ID", asset.get_ID(), "","","");
			ProcessInfoParameter pi3 = new ProcessInfoParameter("A_InternalMovement_ID", no, "","","");
			pi.setParameter(new ProcessInfoParameter[] {pi1, pi2, pi3});
			Trx trx = Trx.get(get_TrxName(), true);	//trx needs to be committed too

			//	Execute Process
			ProcessCtl worker = new ProcessCtl(null, 0, pi, trx);
			worker.start();     //  complete tasks in unlockUI / generateShipments_complete
		}
		log.fine("Transfered "+p_A_Asset_ID);
		return "@Transfered@="+no;
	}	//	doIt
	
}	//	Asset Internal Movement
